define(function(require, exports, module) {
	"use strict";
	document.title="";
	require("../../dep/mui/js/mui.min.js");
	require("../jquery-3.1.1.min.js");
	require("../mui.picker.min.js");
	require("../dateShow.js");
	require("./dateInput.js");
	var muiTips = require("muiTips"); //弹出封装
	var globalInit=require("globalInit");//共用js
	require("keepDate");
	var template = require("template");
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');
	dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏
	var abpUse=require("../abp.js");
	var abp=abpUse.abpstart;
	var examinaSourceId = 0;//启用审批需传参数
	
		mui.init({
				swipeBack:true //启用右滑关闭功能
		});
	
	var getFunList = require("../getFunList/getFunList.js");	//函数配置js
		if(sessionStorage.changeOrderflag){
			getFunList.changeOrderDetail("goodsList","#goodsListShow");				//编辑订单详情														//计算价格
			var orderData= JSON.parse(sessionStorage.getItem("orderInDetail"));
			examinaSourceId = Number(orderData.id); //修改启用审批需传参数
//			sessionStorage.removeItem("flag");
			
		}else{
			getFunList.addGoodsInfoList("goodsList","#goodsListShow");				//新增订单选择货品列表展示
			getFunList.newInstockCode();				//新增入库单号自动生成    客户 业务员自动生成
			getFunList.btnShowAndHide(); //按钮隐藏判断函数
		}
	var sourceType = sessionStorage.getItem("sourceType");
	if(sourceType == abp.sourceType.WH_CHECK){
		$("#mui-choice").hide();
	}
	$(function(){
		
		//设置头部信息
		if(sessionStorage.changeOrderflag) {
			$(".mui-title").html("编辑库存入库");
		} else {
			$(".mui-title").html("新增库存入库");
		}
		dingTalkNav.setTitleText($(".mui-title").html());


		//清除缓存钉钉
		function clearSession(){
			sessionStorage.removeItem("goodsInfoList");	//货品列表信息
	    	sessionStorage.removeItem("chioceStaff");	//业务员信息
	    	sessionStorage.removeItem("chioceStaffId");	
	    	sessionStorage.removeItem("chioceSupplierName");//供应商信息	
	    	sessionStorage.removeItem("chioceSupplierNameId");	
	    	sessionStorage.removeItem("chioceClientName");//客户信息	
	    	sessionStorage.removeItem("chioceClientNameId");
//	    	sessionStorage.removeItem("flag");
	        sessionStorage.removeItem("money");
	        sessionStorage.removeItem('createHome');
		}
		function clearSessionDDUpdate(){			//新增订单页面提交 清除本地存储
	  		clearSession();
	        document.location.href = "../../src/inStock/inStockDetail.html?dd_nav_bgcolor=FFFF943E";
	  	}
		//清除缓存钉钉
		function clearSessionDDCreate(){			//新增订单页面提交 清除本地存储
	  		clearSession();
	        document.location.href = "../../src/inStock/index.html?dd_nav_bgcolor=FFFF943E";
	  	}
		//清除缓存钉钉
		function clearSessionDDHome(){			//新增订单页面提交 清除本地存储
	  		clearSession();
	        document.location.href = "../../src/index.html?dd_nav_bgcolor=FFFF943E";
	  	}
		if(sessionStorage.changeOrderflag){
			$(".mui-title").text("编辑库存入库");
			//钉钉返回清除缓存
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDUpdate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDUpdate);
		}else if(sessionStorage.createHome){
			$(".mui-title").text("新增库存入库");
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDHome);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDHome);
		}else{
			$(".mui-title").text("新增库存入库");
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDCreate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDCreate);
		}
		
		getFunList.settlement();	//计算货品总价
		//删除货品明细列表
		var updateSell = require("./updateSell.js");
		//计算数量与金额 删除明细
		if(sessionStorage.goodsInfoList){
			//计算总数量与金额
//			updateSell.allnumAndAllMoney();
			
			//删除订单明细
			var goodsLists = JSON.parse(sessionStorage.getItem("goodsInfoList"));
//			alert("11");
//			console.log(goodsLists);
			updateSell.deleteGoodsDetail({
				"delete":".delete-trash",		//删除按钮
				"array" : goodsLists,			//需要删除的数组
				"goodsIn" : ".production-list",			//移除页面元素
				"goodsInfoList" : "goodsInfoList" //需要存储的数组对象
			},getFunList.settlement);
		} 
		
		
   
		function newInstock(statusId){                  //请求函数
			var serialNo=$("#code").text().trim();
			var dataCreated=$(".createDate").text().trim();  //创建日期
			var deliveryDate=$(".lastDate").text().trim();	  //预交日期
//			var companyName=$("#companyName").text().trim();
			var busUserId=sessionStorage.chioceStaffId;  //采购员ID
			var notes=$(".remarks").val().trim();
			
			var companyId=0;
			if(sessionStorage.chioceSupplierNameId){
				companyId = sessionStorage.chioceSupplierNameId;
			}
			var option={};
			
	        var dataModel={
	        		"sourceType": 0,		//(integer, optional): 来源单类型 ,
					"sourceSerial": "",	//(string, optional): 来源单号 ,
					"sourceId": 0,		//(integer): 来源单id ,
//					"whInputDetail": ,	//(Array[WhInputDetailInput], optional): 库存入库明细 ,
					"serialNo": serialNo,		//(string): 单号 ,
					"serialDate": dataCreated,		//(string): 单据日期 ,
					"companyId": companyId,		//(integer): 客户 ,
					"contactId": 0,		//(integer): 联系人 ,
					"busUserId": busUserId,		//(integer): 业务员 ,
					"deptId": 0,			//(integer): 部门 ,
					"statusId": statusId,		//(integer): 提交状态 ,
					"notes": notes,			//(string): 备注 ,
					"id": 0,				//(integer, optional): Id
	        	};
	        dataModel.whInputDetail=[];
	        
	        var productionList=$(".production-list");
	        for(var i=0;i<productionList.length;i++){
	        	
	        	var whId=Number($(".production-list").eq(i).find(".warehouse").data("id"));
	        	var goodsId=Number($(".production-list").eq(i).find(".name").data("id"));
	        	var goodsUnit=$(".production-list").eq(i).find(".name").data("unit");
//	        	console.log(goodsUnit)
	        	var price=Number($(".production-list").eq(i).find(".price").text());
				var goodsNum=Number($(".production-list").eq(i).find(".goodsNum").text());
	        	var WhInputDetailInput={
						"whId": whId,			//(integer): 仓库 ,
						"goodsId": goodsId,		//(integer): 货品 ,
						"storageId": 0,		//(integer): 储位 ,
						"unit": goodsUnit,			//(string): 主单位 ,
						"price": price,			//(number): 单价 ,
						"unitCode": goodsUnit,		//(string): 副单位 ,
						"unitCodeNumber": goodsNum,			//(number): 副单位数量 ,		
						"unitCodeConvertRate": 1,	//(number): 副单位转换率 ,
						"notes": "",			//(string, optional): 备注 ,
						"sourceType": 0,		//(integer, optional): 来源单类型 ,
						"sourceSerial": "",	//(string, optional): 来源单编号 ,
						"sourceId": 0,		//(integer, optional): 来源单id ,
						"sourceItemId": 0,	//(integer, optional): 来源单明细id ,
						"batchNo": "",		//(string, optional): 批号 ,
						"expireTime": "",		//(string, optional): 过期时间 ,
						"id": 0,				//(integer, optional): Id
				};
				dataModel.whInputDetail.push(WhInputDetailInput);
				
	        }
	        
	        //console.log(dataModel.whInputDetail);

	        abp.services.app.whInputList.create(dataModel,option)
	            .done(function(data){
	            	//console.log(data);
	            	
	            	function locaHref(){
						document.location.href="../inStock/index.html";
					}
	            	if(dataModel.statusId==abp.statusId.Draft)
		            	{
							muiTips.alertTips(' ',"保存成功",1,3,locaHref);
		            	}
		            	else
		            	{
							muiTips.alertTips(' ',"提交成功",1,3,locaHref);
		            	}
	            	sessionStorage.removeItem('createHome');
	            	getFunList.clearSessionStroage();
	            	
	            })
	            .fail(function(data){
					muiTips.alertTips(' ',data.message,2,3,"default");
					globalInit.buttonReset(".btnDisabledText");
	            });
		}
		
		function changeInstock(statusId){                  //请求函数
			var serialNo=$("#code").text().trim();
			var id=$("#code").attr("data-id");
			var dataCreated=$(".createDate").text().trim();  //创建日期
			var deliveryDate=$(".lastDate").text().trim();	  //预交日期
			var companyId=Number($("#supplier").attr("data-id"));	//客户ID
			var busUserId=Number($("#buyer").attr("data-id"));  //业务员ID
			var notes=$(".remarks").val().trim();
			var option={};
			
	        var dataModel={
	        		"sourceType": 0,		//(integer, optional): 来源单类型 ,
					"sourceSerial": "",	//(string, optional): 来源单号 ,
					"sourceId": 0,		//(integer): 来源单id ,
//					"whInputDetail": ,	//(Array[WhInputDetailInput], optional): 库存入库明细 ,
					"serialNo": serialNo,		//(string): 单号 ,
					"serialDate": dataCreated,		//(string): 单据日期 ,
					"companyId": companyId,		//(integer): 客户 ,
					"contactId": 0,		//(integer): 联系人 ,
					"busUserId": busUserId,		//(integer): 业务员 ,
					"deptId": 0,			//(integer): 部门 ,
					"statusId": statusId,		//(integer): 提交状态 ,
					"notes": notes,			//(string): 备注 ,
					"id": id,				//(integer, optional): Id
	        	};
	        dataModel.whInputDetail=[];
	        
	        var productionList=$(".production-list");
	        for(var i=0;i<productionList.length;i++){
	        	
	        	var whId=Number($(".production-list").eq(i).find(".warehouse").data("id"));
	        	var goodsId=Number($(".production-list").eq(i).find(".name").data("id"));
	        	var goodsUnit=$(".production-list").eq(i).find(".name").data("unit");
	        	var price=Number($(".production-list").eq(i).find(".price").text());
				var goodsNum=Number($(".production-list").eq(i).find(".goodsNum").text());
	        	var WhInputDetailInput={
						"whId": whId,			//(integer): 仓库 ,
						"goodsId": goodsId,		//(integer): 货品 ,
						"storageId": 0,		//(integer): 储位 ,
						"unit": goodsUnit,			//(string): 主单位 ,
						"price": price,			//(number): 单价 ,
						"unitCode": goodsUnit,		//(string): 副单位 ,
						"unitCodeNumber": goodsNum,			//(number): 副单位数量 ,		
						"unitCodeConvertRate": 1,	//(number): 副单位转换率 ,
						"notes": "",			//(string, optional): 备注 ,
						"sourceType": 0,		//(integer, optional): 来源单类型 ,
						"sourceSerial": "",	//(string, optional): 来源单编号 ,
						"sourceId": 0,		//(integer, optional): 来源单id ,
						"sourceItemId": 0,	//(integer, optional): 来源单明细id ,
						"batchNo": "",		//(string, optional): 批号 ,
						"expireTime": "",		//(string, optional): 过期时间 ,
						"id": 0,				//(integer, optional): Id
				};
				dataModel.whInputDetail.push(WhInputDetailInput);
	        }
	        var sourceType = sessionStorage.getItem("sourceType");
	        if(sourceType == abp.sourceType.WH_CHECK){
	        	var orderInDetail = JSON.parse(sessionStorage.getItem("orderInDetail"));
	        	for(var i=0; i< orderInDetail.whInputDetail.length;i++){
	        		
	        		dataModel.whInputDetail[i].sourceType = orderInDetail.whInputDetail[i].sourceType;
	        		dataModel.whInputDetail[i].sourceSerial = orderInDetail.whInputDetail[i].sourceSerial;
	        		dataModel.whInputDetail[i].sourceId = orderInDetail.whInputDetail[i].sourceId;
	        		dataModel.whInputDetail[i].sourceItemId = orderInDetail.whInputDetail[i].sourceItemId;
	        	}
	        }
	        //console.log(dataModel.whInputDetail);

	        abp.services.app.whInputList.update(dataModel,option)
	            .done(function(data){
	            	//console.log(data);
	            
	            	if(dataModel.statusId==abp.statusId.Draft)
		            	{
							muiTips.alertTips(' ',"修改成功",1,3,mui.back);
		            	}
		            	else
		            	{
							muiTips.alertTips(' ',"提交成功",1,3,mui.back);
		            	}
		            	sessionStorage.removeItem('createHome');
		           getFunList.clearSessionStroage();
	            	
	            })
	            .fail(function(data){
					muiTips.alertTips(' ',data.message,2,3,"default");
					globalInit.buttonReset(".btnDisabledText");
	            });
		}
		mui("body").on("tap",".mui-btn-sub",function(){
			globalInit.buttonInit(".mui-btn-sub");
			if(sessionStorage.changeOrderflag){
				changeInstock(abp.statusId.Finish);
			}else{
				newInstock(abp.statusId.Finish);
			}
		});
		mui("body").on("tap",".keep-draft",function(){
			globalInit.buttonInit(".keep-draft");
			if(sessionStorage.changeOrderflag){
				changeInstock(abp.statusId.Draft);
			}else{
				newInstock(abp.statusId.Draft);
			}
		});
		
		mui("body").on("tap",".setParm",function(){
			sessionStorage.removeItem("goodsInfo");
			sessionStorage.removeItem("setParm");
			sessionStorage.setParm=getFunList.parmsNO;
			sessionStorage.money="show";
			sessionStorage.productSelectSource="productSelectSource";
		});
		
		mui("body").on("tap",".mui-action-back",function(){
			//返回 清除本地数据
			sessionStorage.removeItem('createHome');
        	getFunList.clearSessionStroage();
		});
		
		
		
		mui("body").on("tap",".production-list",function(){
			var goodsSerial=$(this).find(".clothes-code").text();
			var goodsName=$(this).find(".name").text();
			var goodsId=$(this).find(".name").attr("data-id");
			var goodsUnit=$(this).find(".name").attr("data-unit");
			var goodsImg=$(this).find(".name").attr("data-img");
			var whName=$(this).find(".warehouse").text();
			var whId=$(this).find(".warehouse").attr("data-id");
			var goodsNumber=$(this).find(".goodsNum").text();
			var price=$(this).find(".price").text();
			var goodsInfo={
					"goodsSerial":goodsSerial,
					"goodsName":goodsName,
					"goodsId":goodsId,
					"goodsUnit":goodsUnit,
					"goodsImg":goodsImg,
					"whName":whName,
					"whId":whId,
					"number":goodsNumber,
					"price":price
				};
			sessionStorage.goodsInfo=JSON.stringify(goodsInfo);
		});
	});
	
	//判断是否启用审批
	var eHistory = require("eHistory");
	var isExamina = eHistory.funIsAuditSetting(abp.sourceType.WH_INPUT, examinaSourceId);
	if(isExamina === true) {
		$("#btnSubmitExamina").text("提交审核");
	} else {
		$("#btnSubmitExamina").text("提交");
	}
		
		
});
